package uit.edu.vn.Activity;


import uit.edu.vn.Utility.MyActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class BackupActivity extends MyActivity {

	///=============
	public Context context;

	private TextView titleHeader;
	private ListView listview_Backup;
    private static final String STRING_NAME_USERSETTING = "USER REFERENCES";
    private SharedPreferences ShareRef = null;
	public String listArrayBackup[]={"Sao Lưu Tập Tin","Sao Lưu Danh Bạ","Sao Lưu Tin Nhắn"};
    private String username = "";
    private String password = "";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_listview);
		
        username = getSetting("username");
        password = getSetting("password");
        //kiem tra neu username va password rong thi ket thuc activity hien tai va goi activity dang nhap
        if(username == "" && password == ""){
            Intent intent = new Intent(BackupActivity.this, LoginActivity.class);
            startActivity(intent);
            this.finish();            	
        }
		context=getBaseContext();
		listview_Backup = (ListView)findViewById(R.id.listview);
		titleHeader=(TextView)findViewById(R.id.txtHeader);
		titleHeader.setText(R.string.title_BackupHeader);
		listview_Backup.setAdapter(new ArrayAdapter<String>(context,R.layout.row_infomain,R.id.txt_rowtext,listArrayBackup));
		
		listview_Backup.setOnItemClickListener(new OnItemClickListener() 
		{
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				// TODO Auto-generated method stub
				// xu ly goi fileview ,contact view ,smsview 
				Intent i ;
				
				if(position==0)
				{
					//ShowToast(String.valueOf(position) );
					// goi fileviewActivity
					i= new Intent().setClass(context, FileBackupActivity.class);
					startActivity(i);
				
				}
				else if(position==1)
				{
					//goi contact view
					i= new Intent().setClass(context, ContactBackupActivity.class);
					startActivity(i);
				}
				else  
				{
					//Goi smsview
					i= new Intent().setClass(context, SMSBackupActivity.class);
					startActivity(i);
				}	
			
			}
		});
	}
	public String getSetting(String key) {
        ShareRef = getSharedPreferences(STRING_NAME_USERSETTING, Context.MODE_WORLD_WRITEABLE);
        return ShareRef.getString(key, "");
	}
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	// TODO Auto-generated method stub
    	if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0)
    	{
    		showConfirmDialog(getResources().getString(R.string.dialog_thongbao),getResources().getString(R.string.dialog_message_thoat) , new OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					logOut();
					finish();
				}
			});
    	}
    	return super.onKeyDown(keyCode, event);
    }
	

}
